Newer
Older
CubeSolver / Code Stuff / PiInterface.py
import ast
import copy

import packages.machine as machine
import packages.cube as cube
import packages.move as move
import sys
import pygame
import time
import sched
import kociemba

if 'Setup Pygame':
    def draw_text(text, x, y, color):
        text_surface = font.render(text, True, color)
        screen.blit(text_surface, (x, y))


    # Function to check if a point is inside a rectangle
    def is_mouse_over(pos, rect):
        return rect.left < pos[0] < rect.right and rect.top < pos[1] < rect.bottom


    # Function to create a button
    def draw_button(rect, color, text):
        pygame.draw.rect(screen, color, rect)
        draw_text(text, rect.x + 10, rect.y + 10, BLACK)


    class Button:
        rect = pygame.Rect
        isPressed = False
        name = "1"

        def draw(self, screen):
            if self.isPressed:
                pygame.draw.rect(screen, (0, 255, 0), self.rect)
            else:
                pygame.draw.rect(screen, (0, 0, 0), self.rect)
            draw_text(self.name, self.rect.x + 10, self.rect.y + 10, (255, 255, 255))

# ___VARIABLES___
# timing stuff
scheduler = sched.scheduler(time.time, time.sleep)
scheduler.run()

# Network Stuff
# pi_ip = '10.42.0.188'
pi_ip = '127.0.0.1'
url = "http://{0}:5000/".format(pi_ip)
# Pygame Stuff
# Define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (150, 150, 150)
screen_width, screen_height = 400, 300

buttons = []

buttonPoss = [
    (130, 10, 50, 50),
    (250, 130, 50, 50),
    (130, 250, 50, 50),
    (10, 130, 50, 50),
    (130, 70, 50, 50),
    (190, 130, 50, 50),
    (130, 190, 50, 50),
    (70, 130, 50, 50),
]

# key grabbing
buttonPressed = 0
number_input = ""

# Misc
# initialize classes
machine = machine.machine(url)
cube = cube.Cube()
# Initialize pygame
pygame.init()

# ___SETUP___
# Set up the screen
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Simple GUI with Pygame")

# Create a font
font = pygame.font.SysFont(None, 40)

# moveLists
setupMoves = move.MoveList(machine, csv_path='moveLists/scan.csv')

# Main function for GUI
if __name__ == "__main__":
    # setup gui
    for i, pos in enumerate(buttonPoss):
        buttons.append(Button())
        buttons[i].rect = pygame.Rect(pos)
        buttons[i].name = str(i)
    # camera = Camera()
    # camera.convert_to_cube_string()
    # exit(0)
    solution = ["Rp", "Bn", "Bn", "Up", "Fn", "Fn", "Dn", "Fn", "Dn", "Bp", "Un", "Rp", "Un", "Un", "Rn", "Rn", "Dp",
                "Fn", "Fn", "Un", "Un", "Fn", "Fn", "Rn", "Rn", "Un", "Un", "Rn", "Rn", "Fn", "Fn"]

    clock = pygame.time.Clock()
    # setupMoves.send_list_request()
    # colours = machine.get_colours()
    colours = [[[19.26974358974359, 83.34051282051281, 129.59794871794873], [164.46966292134832, 152.26516853932586, 176.97303370786517], [163.5367231638418, 48.13446327683616, 63.10960451977401], [2.6155196629213484, 143.1936797752809, 26.899719101123594], [13.30875, 113.929375, 36.19625], [227.25509868421054, 94.89226973684211, 60.01027960526316], [193.675, 80.9875, 62.13409090909091], [22.031868131868134, 72.64505494505495, 119.65934065934066], [175.7927786499215, 154.07142857142858, 184.9120879120879]], [[54.97025641025641, 123.38666666666667, 43.06153846153846], [175.02097378277153, 150.3056179775281, 175.95730337078652], [164.66101694915255, 143.19887005649719, 170.81016949152541], [201.55891853932584, 190.5191011235955, 37.26636235955056], [167.87375, 80.221875, 52.2075], [46.561677631578945, 90.91973684210527, 163.14383223684212], [164.22272727272727, 151.3, 53.28409090909091], [45.15714285714286, 68.9032967032967, 115.05714285714286], [172.58634222919937, 141.787284144427, 177.7951334379906]], [[158.33333333333334, 152.81641025641025, 63.98871794871795], [169.42846441947566, 49.161048689138575, 78.1558052434457], [184.94915254237287, 88.29943502824858, 71.5909604519774], [37.749087078651684, 93.24431179775281, 162.00365168539327], [144.206875, 127.104375, 154.109375], [202.00888157894738, 13.303865131578947, 57.404111842105266], [169.2068181818182, 133.9852272727273, 168.44431818181818], [167.2010989010989, 72.52857142857142, 60.497802197802194], [177.01412872841445, 39.61773940345369, 68.6342229199372]], [[154.12923076923076, 24.49025641025641, 47.505641025641026], [8.232958801498127, 127.19475655430712, 47.421722846441945], [10.04858757062147, 80.22598870056497, 130.68926553672316], [191.58918539325842, 13.671629213483145, 52.75498595505618], [23.83625, 71.448125, 122.02625], [2.6790296052631577, 144.93651315789472, 34.22129934210526], [36.30795454545454, 78.01022727272728, 132.0159090909091], [140.67252747252746, 122.32307692307693, 147.24945054945056], [168.1828885400314, 161.65149136577708, 59.6436420722135]], [[45.05948717948718, 77.37538461538462, 127.04717948717949], [167.5505617977528, 147.59101123595505, 174.8486891385768], [33.616949152542375, 124.56497175141243, 41.9728813559322], [195.0868679775281, 11.585323033707866, 37.41994382022472], [145.603125, 28.815, 46.724375], [17.184703947368423, 147.0141447368421, 18.533141447368422], [54.657954545454544, 119.45113636363637, 40.925], [146.1010989010989, 138.87692307692308, 42.55604395604396], [203.20800627943484, 94.10204081632654, 68.76923076923077]], [[51.09641025641026, 115.78358974358974, 29.117948717948718], [193.57977528089887, 88.43895131086143, 50.205243445692886], [191.42372881355934, 84.62824858757062, 48.91864406779661], [196.06327247191012, 182.5631320224719, 25.92808988764045], [146.165625, 136.5825, 34.326875], [203.90057565789473, 184.34432565789473, 23.263240131578947], [159.6909090909091, 35.23977272727273, 41.04318181818182], [173.23406593406594, 72.04835164835166, 46.08571428571429], [171.8877551020408, 157.5879120879121, 44.10596546310832]]]
    print(1)
    cube.make_cube_string(colours)
    print(2)
    # print(kociemba.solve(cube.cube_string))
    #machine.solve_cube(cube.cube_string)

    # setupMoves.execute()
    # scanMoves.execute()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    # try:
                    #     num = int(number_input)
                    #     moves2.counter = num
                    #     moves2.execute()
                    # except:
                    #     pass

                    machine.set_angle(buttonPressed, number_input)
                    # print("Entered number:", number_input)
                    data_to_send = {
                        "component": "servo",
                        "index": buttonPressed,
                        "value": number_input,
                    }
                    number_input = ""
                    # response = requests.post(url, data=data_to_send)
                elif event.key == pygame.K_BACKSPACE:
                    number_input = number_input[:-1]
                elif event.key == pygame.K_UP:
                    buttonPressed = 2
                elif event.key == pygame.K_RIGHT:
                    buttonPressed = 3
                elif event.key == pygame.K_DOWN:
                    buttonPressed = 0
                elif event.key == pygame.K_LEFT:
                    buttonPressed = 1
                else:
                    number_input += event.unicode
                if buttonPressed < 4:
                    if event.key == pygame.K_r:
                        machine.retract(buttonPressed)
                    elif event.key == pygame.K_e:
                        machine.extend(buttonPressed)
                    elif event.key == pygame.K_t:
                        machine.full_extend(buttonPressed)
                    elif event.key == pygame.K_a:
                        machine.goto(buttonPressed, 0)
                    elif event.key == pygame.K_s:
                        machine.goto(buttonPressed, 1)
                    elif event.key == pygame.K_d:
                        machine.goto(buttonPressed, 2)
                    elif event.key == pygame.K_f:
                        machine.goto(buttonPressed, 3)
                    elif event.key == pygame.K_q:
                        machine.move(buttonPressed, 1)
                    elif event.key == pygame.K_w:
                        machine.move(buttonPressed, -1)
                    elif event.key == pygame.K_1:
                        cube.rotate_side('U', 1)
                    elif event.key == pygame.K_2:
                        cube.rotate_side('L', 1)
                    elif event.key == pygame.K_3:
                        cube.rotate_side('F', 1)
                    elif event.key == pygame.K_4:
                        cube.rotate_side('R', 1)
                    elif event.key == pygame.K_5:
                        cube.rotate_side('B', 1)
                    elif event.key == pygame.K_6:
                        cube.rotate_side('D', 1)

            elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                mouse_pos = pygame.mouse.get_pos()
                for i, button in enumerate(buttons):
                    button.isPressed = False
                    if is_mouse_over(mouse_pos, button.rect):
                        buttonPressed = i
                        button.isPressed = True
                # # Check if the left mouse button is clicked on the buttons
                if is_mouse_over(mouse_pos, pygame.Rect(150, 150, 100, 50)):
                    print("Button 1 clicked!")
                elif is_mouse_over(mouse_pos, pygame.Rect(150, 220, 100, 50)):
                    print("Button 2 clicked!")

        # tick the servos
        # machine.tick()
        # setupMoves.tick()
        # moves2.tick()
        # response = scanMoves.tick()
        # if response:
        #     cube.cube_string.append(response)
        #     print(cube.cube_string)
        machine.tick()
        # Clear the screen
        screen.fill(GRAY)

        # Draw the input field
        #pygame.draw.rect(screen, GRAY, pygame.Rect(10, 10, 200, 40))
        draw_text(number_input, 20, 20, BLACK)

        #pygame.draw.rect(screen, GRAY, pygame.Rect(10, 10, 200, 40))
        # draw the cube net
        colourSideDict = {
            "U": (255, 255, 255),
            "F": (0, 255, 0),
            "L": (255, 0, 0),
            "R": (255, 128, 0),
            "B": (0, 0, 255),
            "D": (255, 255, 0)
        }
        cube_index_positions = [
            [3, 0], [4, 0], [5, 0],
            [3, 1], [4, 1], [5, 1],
            [3, 2], [4, 2], [5, 2],

            [6, 3], [7, 3], [8, 3],
            [6, 4], [7, 4], [8, 4],
            [6, 5], [7, 5], [8, 5],

            [3, 3], [4, 3], [5, 3],
            [3, 4], [4, 4], [5, 4],
            [3, 5], [4, 5], [5, 5],

            [3, 6], [4, 6], [5, 6],
            [3, 7], [4, 7], [5, 7],
            [3, 8], [4, 8], [5, 8],

            [0, 3], [1, 3], [2, 3],
            [0, 4], [1, 4], [2, 4],
            [0, 5], [1, 5], [2, 5],

            [9, 3], [10, 3], [11, 3],
            [9, 4], [10, 4], [11, 4],
            [9, 5], [10, 5], [11, 5],
        ]
        for i, item in enumerate(cube.cube_string):
            pygame.draw.rect(screen, colourSideDict[item], pygame.Rect(
                20*cube_index_positions[i][0],
                20*cube_index_positions[i][1], 20, 20))

        # Draw buttons
        #for button in buttons:
            #button.draw(screen)
        # draw_button(pygame.Rect(150, 150, 100, 50), BLACK, "Button 1")
        # draw_button(pygame.Rect(150, 220, 100, 50), BLACK, "Button 2")

        # Update the display
        pygame.display.flip()

        clock.tick(20)